package com.yssoft.service.supervise.inspection;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.yssoft.entity.inspection.TInspProductCly;
import com.yssoft.entity.inspection.TInspStandard;
import com.yssoft.entity.inspection.TInspStandardDetail;
import com.yssoft.entity.inspection.TInspStandardEnt;
import com.yssoft.mapper.inspection.TInspProductClyMapper;
import com.yssoft.mapper.inspection.TInspStandardDetailMapper;
import com.yssoft.mapper.inspection.TInspStandardMapper;
import com.yssoft.utils.LayuiUtil;
import com.yssoft.utils.UserHelper;

@Service
public class SuInspectionService {

	@Resource
	private TInspStandardMapper tInspStandardMapper;
	@Resource
	private TInspStandardDetailMapper tInspStandardDetailMapper;
	@Resource
	private TInspProductClyMapper tInspProductClyMapper;
	
	/**
	 * 查询监管-检验标准列表
	 * @author 戈超
	 * @param page
	 * @param limit
	 * @param code
	 * @param name
	 * @param productClassifyId
	 * @return
	 */
	public JSONObject queryInspectionPage(int page, int limit, String code, String name, Integer productClassifyId) {
		//获取当前登陆账号企业id
        /*Integer entId =UserHelper.getCurrentCompanyId();*/
		//下面第一个查询结果进行分页
        PageHelper.startPage(page,limit);
        //整理查询条件
        Map<String,Object> param=new HashMap<>();
        param.put("name", name);
        param.put("code", code);
        param.put("productClassifyId", productClassifyId);
        List<Map<String,Object>> personList = tInspStandardMapper.queryStandardList(param);
        //查询条数
        int count = tInspStandardMapper.selectCountSql(param);
        JSONObject jsonObject = LayuiUtil.getJSONObjectByMap(personList,count,"","");
		return jsonObject;
	}
	
	/**
	 * 查询监管-产品类型列表
	 * @param page
	 * @param limit
	 * @param name
	 * @return
	 */
	public JSONObject queryClassifyList(int page, int limit, String name) {
		//下面第一个查询结果进行分页
        PageHelper.startPage(page,limit);
        //整理查询条件
        Map<String,Object> param=new HashMap<>();
        param.put("name", name);
        List<Map<String,Object>> personList = tInspStandardMapper.queryClassifyList(param);
        //查询条数
        int count = tInspStandardMapper.queryClassifyCount(param);
        JSONObject jsonObject = LayuiUtil.getJSONObjectByMap(personList,count,"","");
		return jsonObject;
	}

	/**
	 * 保存监管-检验标准新增保存
	 * @author 戈超
	 * @param tInspStandard
	 * @param delIds 
	 */
	@Transactional(rollbackFor = Exception.class)
	public void saveInspStandard(TInspStandard tInspStandard, String delIds) {
		/*删除去除检验标准详细*/
		if(delIds != null && delIds.length() > 0){
			String[] delId = delIds.split(",");
			for(String dId : delId){
				TInspStandardDetail tInspStandardDetail1 = new TInspStandardDetail();
				tInspStandardDetail1.setDeleteFlag("1");
				tInspStandardDetail1.setId(Integer.parseInt(dId));
				tInspStandardDetailMapper.updateByPrimaryKeySelective(tInspStandardDetail1);
			}
		}
		int sum = 0;
		/*整理检验标准数据*/
		if(tInspStandard.getId() == null){
			tInspStandard.setDeleteFlag("0");
			tInspStandard.setCreateTime(new Date());
			tInspStandard.setCreatorId(UserHelper.getCurrentUser().getPersonId());
			tInspStandard.setCreatorName(UserHelper.getCurrentUser().getPersonName());
			tInspStandard.setDeptId(UserHelper.getCurrentUser().getDeptId());
			tInspStandard.setDeptName(UserHelper.getCurrentUser().getDeptName());
			/*保存检验标准*/
			sum = tInspStandardMapper.insertSelective(tInspStandard);
		} else {
			/*删除产品类型信息*/
			Map<String, Object> delMap = new HashMap<>();
			delMap.put("inspId", tInspStandard.getId());
			tInspProductClyMapper.updateDeleteFlag(delMap);
			/*编辑检验标准*/
			sum = tInspStandardMapper.updateByPrimaryKeySelective(tInspStandard);
		}
		
		if(sum > 0){
			/*获取检验标准id*/
			Integer id = tInspStandard.getId();
			/*保存产品类型*/
			String[] classifyId = tInspStandard.getClassifyIds().split(",");
			for(int i = 0; i < classifyId.length; i++){
				TInspProductCly tInspProductCly = new TInspProductCly();
				tInspProductCly.setDeleteFlag("0");
				tInspProductCly.setInspId(id);
				tInspProductCly.setProductClyId(Integer.parseInt(classifyId[i]));
				tInspProductClyMapper.insertSelective(tInspProductCly);
			}
			/*判断检验步骤*/
			boolean isInspStep = false;
			if(tInspStandard.getInsp_step() != null && tInspStandard.getInsp_step().length > 0){
				isInspStep = true;
			}
			/*保存检验标准详细*/
			if(tInspStandard.getDetailName() != null && tInspStandard.getDetailName().length > 0){
				/*基础数据整理*/
				TInspStandardDetail tInspStandardDetail = new TInspStandardDetail();
				for(int i = 0; i < tInspStandard.getDetailName().length; i++){
					tInspStandardDetail.setId(null);
					tInspStandardDetail.setName(tInspStandard.getDetailName()[i]);
					tInspStandardDetail.setMinValue(tInspStandard.getMin_value()[i]);
					tInspStandardDetail.setMaxValue(tInspStandard.getMax_value()[i]);
					tInspStandardDetail.setUnit(tInspStandard.getUnit()[i]);
					if(isInspStep){
						tInspStandardDetail.setInspStep(tInspStandard.getInsp_step()[i]);
					}
					if("newId".equals(tInspStandard.getStandardDetailId()[i])){
						tInspStandardDetail.setDeleteFlag("0");
						tInspStandardDetail.settInspStandardId(id);
						tInspStandardDetail.setCreateTime(new Date());
						tInspStandardDetail.setCreatorId(UserHelper.getCurrentUser().getPersonId());
						tInspStandardDetail.setCreatorName(UserHelper.getCurrentUser().getPersonName());
						tInspStandardDetailMapper.insertSelective(tInspStandardDetail);
					} else {
						tInspStandardDetail.setId(Integer.parseInt(tInspStandard.getStandardDetailId()[i]));
						tInspStandardDetailMapper.updateByPrimaryKeySelective(tInspStandardDetail);
					}
				}
			}
			
		}
	}

	/**
	 * 查询监管-检验标准信息查询ById
	 * @author 戈超
	 * @param tInspStandard
	 */
	public TInspStandard queryInspectionById(Integer inspectionId) {
		/*整理查询条件*/
		TInspStandard tInspStandard = new TInspStandard();
		tInspStandard.setId(inspectionId);
		tInspStandard.setDeleteFlag("0");
		/*查询检验标准信息*/
		tInspStandard = tInspStandardMapper.selectByPrimaryKey(tInspStandard);
		/*整理检验标准详细查询条件*/
		TInspStandardDetail tInspStandardDetail = new TInspStandardDetail();
		tInspStandardDetail.setDeleteFlag("0");
		tInspStandardDetail.settInspStandardId(tInspStandard.getId());
		/*查询检验标准详细信息*/
		List<TInspStandardDetail> tInspStandardDetails = tInspStandardDetailMapper.select(tInspStandardDetail);
		tInspStandard.settInspStandardDetails(tInspStandardDetails);
		/*查询产品类型信息*/
		Map<String, Object> map = tInspStandardMapper.queryClassify(tInspStandard.getId());
		tInspStandard.setClassifyIds(map.get("ids").toString());
		tInspStandard.setClassifyNames(map.get("names").toString());
		return tInspStandard;
	}

	/**
	 * 删除监管-检验管理删除
	 * @param ids
	 * @return
	 */
	public void delProcess(String processId) {
		TInspStandard tInspStandard = new TInspStandard();
		tInspStandard.setId(Integer.parseInt(processId));
		tInspStandard.setDeleteFlag("1");
		tInspStandardMapper.updateByPrimaryKeySelective(tInspStandard);
	}
}
